home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bison / bisn119s.zoo / files.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  9.9 KB  |  408 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include "system.h"
  34. #include "files.h"
  35. #include "new.h"
  36. #include "gram.h"
  37.  
  38. FILE *finput = NULL;
  39. FILE *foutput = NULL;
  40. FILE *fdefines = NULL;
  41. FILE *ftable = NULL;
  42. FILE *fattrs = NULL;
  43. FILE *fguard = NULL;
  44. FILE *faction = NULL;
  45. FILE *fparser = NULL;
  46.  
  47. /* File name specified with -o for the output file, or 0 if no -o.  */
  48. char *spec_outfile;
  49.  
  50. char *infile;
  51. char *outfile;
  52. char *defsfile;
  53. char *tabfile;
  54. char *attrsfile;
  55. char *guardfile;
  56. char *actfile;
  57. char *tmpattrsfile;
  58. char *tmptabfile;
  59. char *tmpdefsfile;
  60.  
  61. extern char    *mktemp();    /* So the compiler won't complain */
  62. extern char    *getenv();
  63. extern void    perror();
  64. extern void    exit();
  65. FILE    *tryopen();    /* This might be a good idea */
  66. void done();
  67.  
  68. extern char *program_name;
  69. extern int verboseflag;
  70. extern int definesflag;
  71. int fixed_outfiles = 0;
  72. #ifdef atarist
  73. long _stksize = -1L;
  74. #endif
  75.  
  76.  
  77. char*
  78. stringappend(string1, end1, string2)
  79. char *string1;
  80. int end1;
  81. char *string2;
  82. {
  83.   register char *ostring;
  84.   register char *cp, *cp1;
  85.   register int i;
  86.  
  87.   cp = string2;  i = 0;
  88.   while (*cp++) i++;
  89.  
  90.   ostring = NEW2(i+end1+1, char);
  91.  
  92.   cp = ostring;
  93.   cp1 = string1;
  94.   for (i = 0; i < end1; i++)
  95.     *cp++ = *cp1++;
  96.  
  97.   cp1 = string2;
  98.   while (*cp++ = *cp1++) ;
  99.  
  100.   return ostring;
  101. }
  102.  
  103.  
  104. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  105.    the output files, and opens the tmp files and the parser */
  106. void
  107. openfiles()
  108. {
  109.   char *name_base;
  110.   register char *cp;
  111.   char *filename;
  112.   int base_length;
  113.   int short_base_length;
  114.  
  115. #ifdef VMS
  116.   char *tmp_base = "sys$scratch:b_";
  117. #else
  118.   char *tmp_base = "/tmp/b.";
  119. #endif
  120.   int tmp_len;
  121.  
  122. #if defined(MSDOS) || defined(atarist)
  123.   tmp_base = getenv ("TEMP");
  124.   if(tmp_base == 0) tmp_base = getenv("TMP");
  125.   if (tmp_base == 0)
  126.     tmp_base = "";
  127.   strlwr (infile);
  128. #endif /* MSDOS || atarist */
  129.  
  130.   tmp_len = strlen (tmp_base);
  131.  
  132.   if (spec_outfile)
  133.     {
  134.       /* -o was specified.  The precise -o name will be used for ftable.
  135.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  136.       name_base = spec_outfile;
  137. #if defined(MSDOS) || defined(atarist)
  138.       strlwr (name_base);
  139. #endif /* MSDOS || atarist */
  140.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  141.       base_length = strlen (name_base);
  142.       if (!strcmp (name_base + base_length - 2, ".c"))
  143.     base_length -= 2;
  144.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  145.       short_base_length = base_length;
  146.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  147.     short_base_length -= 4;
  148.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  149.     short_base_length -= 4;
  150.     }
  151.   else if (spec_file_prefix)
  152.     {
  153.       /* -b was specified.  Construct names from it.  */
  154.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  155.       short_base_length = strlen (spec_file_prefix);
  156.       /* Count room for `.tab'.  */
  157.       base_length = short_base_length + 4;
  158.       name_base = (char *) mallocate (base_length + 1);
  159.       /* Append `.tab'.  */
  160.       strcpy (name_base, spec_file_prefix);
  161. #ifdef VMS
  162.       strcat (name_base, "_tab");
  163. #else
  164.       strcat (name_base, ".tab");
  165. #endif
  166. #if defined(MSDOS) || defined(atarist)
  167.       strlwr (name_base);
  168. #endif /* MSDOS */
  169.     }
  170.   else
  171.     {
  172.       /* -o was not specified; compute output file name from input
  173.      or use y.tab.c, etc., if -y was specified.  */
  174.  
  175.       name_base = fixed_outfiles ? "y.y" : infile;
  176.  
  177.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  178.  
  179.       base_length = strlen (name_base);
  180.       if (!strcmp (name_base + base_length - 2, ".y"))
  181.     base_length -= 2;
  182.       short_base_length = base_length;
  183.  
  184. #ifdef VMS
  185.       name_base = stringappend(name_base, short_base_length, "_tab");
  186. #else
  187. #if defined(MSDOS) || defined(atarist)
  188.       name_base = stringappend(name_base, short_base_length, "_tab");
  189. #else
  190.       name_base = stringappend(name_base, short_base_length, ".tab");
  191. #endif /* not MSDOS || atarist */
  192. #endif
  193.       base_length = short_base_length + 4;
  194.     }
  195.  
  196.   finput = tryopen(infile, "r");
  197.  
  198.   filename = getenv("BISON_SIMPLE");
  199. #if defined(MSDOS) || defined(atarist)
  200.   /* File doesn't exist in current directory; try in INIT directory.  */
  201. #ifdef atarist
  202.   cp = getenv("GNULIB");
  203. #else
  204.   cp = getenv("INIT");
  205. #endif
  206.   if (filename == 0 && cp != 0)
  207.     {
  208.       filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  209.       strcpy(filename, cp);
  210.       cp = filename + strlen(filename);
  211.       *cp++ = '/';
  212.       strcpy(cp, PFILE);
  213.     }
  214. #endif /* MSDOS */
  215.   fparser = tryopen(filename ? filename : PFILE, "r");
  216.  
  217.   if (verboseflag)
  218.     {
  219. #if defined(MSDOS) || defined(atarist)
  220.       outfile = stringappend(name_base, short_base_length, ".out");
  221. #else
  222.       if (spec_name_prefix)
  223.     outfile = stringappend(name_base, short_base_length, ".out");
  224.       else
  225.     outfile = stringappend(name_base, short_base_length, ".output");
  226. #endif
  227.       foutput = tryopen(outfile, "w");
  228.     }
  229.  
  230. #if defined(MSDOS) || defined(atarist)
  231.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  232.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  233.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  234.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX"));
  235. #else
  236.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  237.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  238.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  239.   tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX"));
  240. #endif /* not MSDOS */
  241.  
  242.   faction = tryopen(actfile, "w+");
  243.   fattrs = tryopen(tmpattrsfile,"w+");
  244.   ftable = tryopen(tmptabfile, "w+");
  245.  
  246.   if (definesflag)
  247.     {
  248.       defsfile = stringappend(name_base, base_length, ".h");
  249.       fdefines = tryopen(tmpdefsfile, "w+");
  250.     }
  251.  
  252. #if !(defined(MSDOS) || defined(atarist))
  253.   unlink(actfile);
  254.   unlink(tmpattrsfile);
  255.   unlink(tmptabfile);
  256.   unlink(tmpdefsfile);
  257. #endif
  258.  
  259.     /* These are opened by `done' or `open_extra_files', if at all */
  260.   if (spec_outfile)
  261.     tabfile = spec_outfile;
  262.   else
  263.     tabfile = stringappend(name_base, base_length, ".c");
  264.  
  265. #ifdef VMS
  266.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  267.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  268. #else
  269. #if defined(MSDOS) || defined(atarist)
  270.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  271.   guardfile = stringappend(name_base, short_base_length, ".guc");
  272. #else
  273.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  274.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  275. #endif /* not MSDOS */
  276. #endif /* not VMS */
  277. }
  278.  
  279.  
  280.  
  281. /* open the output files needed only for the semantic parser.
  282. This is done when %semantic_parser is seen in the declarations section.  */
  283.  
  284. void
  285. open_extra_files()
  286. {
  287.   FILE *ftmp;
  288.   int c;
  289.   char *filename, *cp;
  290.  
  291.   fclose(fparser);
  292.  
  293.   filename = (char *) getenv ("BISON_HAIRY");
  294. #if defined(MSDOS) || defined(atarist)
  295.   /* File doesn't exist in current directory; try in INIT directory.  */
  296. #ifdef atarist
  297.   cp = getenv("GNULIB");
  298. #else
  299.   cp = getenv("INIT");
  300. #endif
  301.   if (filename == 0 && cp != 0)
  302.     {
  303.       filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  304.       strcpy(filename, cp);
  305.       cp = filename + strlen(filename);
  306.       *cp++ = '/';
  307.       strcpy(cp, PFILE1);
  308.     }
  309. #endif
  310.   fparser= tryopen(filename ? filename : PFILE1, "r");
  311.  
  312.         /* JF change from inline attrs file to separate one */
  313.   ftmp = tryopen(attrsfile, "w");
  314.   rewind(fattrs);
  315.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  316.     putc(c,ftmp);
  317.   fclose(fattrs);
  318.   fattrs=ftmp;
  319.  
  320.   fguard = tryopen(guardfile, "w");
  321.  
  322. }
  323.  
  324.     /* JF to make file opening easier.  This func tries to open file
  325.        NAME with mode MODE, and prints an error message if it fails. */
  326. FILE *
  327. tryopen(name, mode)
  328. char *name;
  329. char *mode;
  330. {
  331.   FILE    *ptr;
  332.  
  333.   ptr = fopen(name, mode);
  334.   if (ptr == NULL)
  335.     {
  336.       fprintf(stderr, "%s: ", program_name);
  337.       perror(name);
  338.       done(2);
  339.     }
  340.   return ptr;
  341. }
  342.  
  343. void
  344. done(k)
  345. int k;
  346. {
  347.   if (faction)
  348.     fclose(faction);
  349.  
  350.   if (fattrs)
  351.     fclose(fattrs);
  352.  
  353.   if (fguard)
  354.     fclose(fguard);
  355.  
  356.   if (finput)
  357.     fclose(finput);
  358.  
  359.   if (fparser)
  360.     fclose(fparser);
  361.  
  362.   if (foutput)
  363.     fclose(foutput);
  364.  
  365.     /* JF write out the output file */
  366.   if (k == 0 && ftable)
  367.     {
  368.       FILE *ftmp;
  369.       register int c;
  370.  
  371.       ftmp=tryopen(tabfile, "w");
  372.       rewind(ftable);
  373.       while((c=getc(ftable)) != EOF)
  374.         putc(c,ftmp);
  375.       fclose(ftmp);
  376.       fclose(ftable);
  377.  
  378.       if (definesflag)
  379.         {
  380.           ftmp = tryopen(defsfile, "w");
  381.           fflush(fdefines);
  382.           rewind(fdefines);
  383.           while((c=getc(fdefines)) != EOF)
  384.             putc(c,ftmp);
  385.           fclose(ftmp);
  386.           fclose(fdefines);
  387.         }
  388.     }
  389.  
  390. #ifdef VMS
  391.   if (faction)
  392.     delete(actfile);
  393.   if (fattrs)
  394.     delete(tmpattrsfile);
  395.   if (ftable)
  396.     delete(tmptabfile);
  397.   if (k==0) sys$exit(SS$_NORMAL);
  398.   sys$exit(SS$_ABORT);
  399. #else
  400. #if defined(MSDOS) || defined(atarist)
  401.   if (actfile) unlink(actfile);
  402.   if (tmpattrsfile) unlink(tmpattrsfile);
  403.   if (tmptabfile) unlink(tmptabfile);
  404. #endif /* MSDOS */
  405.   exit(k);
  406. #endif /* not VMS */
  407. }
  408.